package cn.zurish.cloud.security.service;

import lombok.RequiredArgsConstructor;

@Service
@RequiredArgsConstructor
public class AuthService {

    private final AuthenticationManager authenticationManager;
    private final JwtUtils jwtUtils;

    public AuthResponse authenticate(LoginRequest request) {
        // authenticationManager会调用 userDetailsService.loadUserByUsername 方法
        Authentication authentication = authenticationManager
                .authenticate(new UsernamePasswordAuthenticationToken(request.getUsername(), request.getPassword()));
        SysUser user = (SysUser)authentication.getPrincipal();

        String token = jwtUtils.generateToken(user.getUsername());
        return new AuthResponse(token, user.getUsername(), user.getAuthorities());
    }
}
